﻿Imports System.Data.SqlClient
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports Guna.UI2.WinForms

Public Class 财务支出
    Private Sub 确定btn_Click(sender As Object, e As EventArgs) Handles 确定btn.Click
        ' 1️⃣ 校验输入
        If 支出方式_data.SelectedValue Is Nothing Then
            MessageBox.Show("请选择支出方式！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            支出方式_data.Focus()
            Exit Sub
        End If

        If 支出金额_data.Value <= 0 Then
            MessageBox.Show("支出金额必须大于0！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            支出金额_data.Focus()
            Exit Sub
        End If

        If String.IsNullOrWhiteSpace(收款人_data.Text) Then
            MessageBox.Show("请输入收款人！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            收款人_data.Focus()
            Exit Sub
        End If

        ' 可选参数
        Dim memo As String = RichTextBox1.Text.Trim()
        Dim dd As String = 地点_data.Text.Trim()

        Try
            Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
                conn.Open()

                Using cmd As New SqlCommand("usp_AddCwZC", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    ' 传入参数
                    cmd.Parameters.AddWithValue("@ZCSJ", 支出时间_data.Value)
                    cmd.Parameters.AddWithValue("@ZCFS", 支出方式_data.SelectedValue)
                    cmd.Parameters.AddWithValue("@ZCFSMC", 支出方式_data.Text)
                    cmd.Parameters.AddWithValue("@MEMO", If(String.IsNullOrEmpty(memo), DBNull.Value, memo))
                    cmd.Parameters.AddWithValue("@DD", If(String.IsNullOrEmpty(dd), DBNull.Value, dd))
                    cmd.Parameters.AddWithValue("@SKF", 收款人_data.Text.Trim())
                    cmd.Parameters.AddWithValue("@ZCJE", 支出金额_data.Value)

                    ' 读取存储过程返回结果
                    Dim resultMsg As String = ""
                    Using reader As SqlDataReader = cmd.ExecuteReader()
                        If reader.Read() Then
                            resultMsg = reader("Msg").ToString()
                        End If
                    End Using

                    MessageBox.Show(resultMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    ' 清空输入框
                    支出金额_data.Value = 0
                    RichTextBox1.Clear()
                    地点_data.Clear()
                    收款人_data.Clear()
                    支出方式_data.SelectedIndex = -1
                End Using
            End Using

        Catch ex As Exception
            LogHelper.WriteLog("执行支出出错：" & ex.Message)
            MessageBox.Show("执行出错：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


    Private Sub 取消btn_Click(sender As Object, e As EventArgs) Handles 取消btn.Click
        Me.Close()
    End Sub
    Private borderless As Guna2BorderlessForm
    Private Sub 财务支出_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 创建并配置 Guna2BorderlessForm
        'borderless = New Guna2BorderlessForm()
        'borderless.TargetForm = Me
        'borderless.BorderRadius = 20                   ' 圆角半径
        'borderless.ShadowColor = Color.Gray           ' 阴影颜色
        'borderless.ShadowDepth = 50                   ' 阴影深度
        'borderless.TransparentWhileDrag = True        ' 拖动透明
        'borderless.DockIndicatorTransparencyValue = 0.6





        ' 假设控件名为 支出时间_data
        支出时间_data.Format = DateTimePickerFormat.Custom
        支出时间_data.CustomFormat = "yyyy-MM-dd HH:mm"   ' 显示年月日 + 小时分钟
        支出时间_data.ShowUpDown = True                   ' 使用上下箭头选择时间，而不是下拉日历


        loadZffsData()
    End Sub

    Function loadZffsData()
        Dim dt As New DataTable()

        Try
            Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
                Dim sql As String = "SELECT * FROM ZCFSK where JLZT=1"
                Dim da As New SqlDataAdapter(sql, conn)
                da.Fill(dt)
            End Using

            ' 绑定到 ComboBox
            支出方式_data.DataSource = dt
            支出方式_data.DisplayMember = "ZCFS"  ' 显示字段
            支出方式_data.ValueMember = "ID"      ' 值字段

        Catch ex As Exception
            MessageBox.Show("加载支出方式数据失败：" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function
End Class